import { Component, h, Method, Prop, Watch } from '@stencil/core';

/**
 * 拷贝
 *
 * @author chitanda
 * @date 2021-06-22 15:06:49
 * @export
 * @class IBzTextCopy
 */
@Component({
  tag: 'ibz-text-copy',
  styleUrl: 'ibz-text-copy.scss',
  shadow: true,
})
export class IBzTextCopy {
  ref: HTMLInputElement;

  /**
   * 需要拷贝至粘贴板的文本
   *
   * @author chitanda
   * @date 2022-06-16 10:06:35
   * @type {string}
   */
  @Prop()
  text: string = '';
  @Watch('text')
  watchText() {
    this.copy(this.text);
  }

  @Method()
  async copy(text: string): Promise<boolean> {
    this.ref.value = text;
    this.ref.select();
    const judge = document.execCommand('copy', false, text);
    if (judge === true) {
      return true;
    } else {
      return false;
    }
  }

  render() {
    return <input type='text' ref={ref => (this.ref = ref)} />;
  }
}
